package com.aiti.myproject.client.widgets;

import java.util.List;

import com.aiti.myproject.client.events.GalleryUpdatedEvent;
import com.aiti.myproject.client.events.GalleryUpdatedEventHandler;
import com.aiti.myproject.client.service.UserImageService;
import com.aiti.myproject.client.service.UserImageServiceAsync;
import com.aiti.myproject.shared.LoginInfo;
import com.aiti.myproject.shared.Tag;
import com.aiti.myproject.shared.UploadImage;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ImageOverlay extends Composite implements HasHandlers {

	private static ImageOverlayUiBinder uiBinder = GWT
			.create(ImageOverlayUiBinder.class);

	UserImageServiceAsync imageService = GWT.create(UserImageService.class);

	private HandlerManager handlerManager;

	interface ImageOverlayUiBinder extends UiBinder<Widget, ImageOverlay> {
	}

	@UiField
	Image image;

	@UiField
	VerticalPanel tagPanel;

	protected UploadImage uploadeImage;
	LoginInfo loginInfo;

	public ImageOverlay(UploadImage uploadedImage, LoginInfo loginInfo) {
		handlerManager = new HandlerManager(this);

		this.uploadeImage = uploadedImage;
		this.loginInfo = loginInfo;

		initWidget(uiBinder.createAndBindUi(this));

		image.setUrl(uploadedImage.getServingUrl());

		imageService.getTagsForImage(uploadedImage,
				new AsyncCallback<List<Tag>>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.toString());
					}

					@Override
					public void onSuccess(List<Tag> result) {
						for (Tag tag : result) {
							tagPanel.add(new HTMLPanel(tag.getBody()));
						}

					}
				});

	}

	@UiHandler("image")
	void onClickImage(MouseDownEvent e) {
		Element imageElement = e.getRelativeElement();
		int x = e.getRelativeX(imageElement);
		int y = e.getRelativeY(imageElement);
		TagDialog tagDialog = new TagDialog(image, x, y);
		tagDialog.showAndFocus();

	}

	private class TagDialog extends DialogBox {

		private TextBox textBox;

		public TagDialog(Image image, final int x, final int y) {

			setText("Tagging X: " + x + " Y: " + y);

			setAutoHideEnabled(true);

			int dialogX = image.getAbsoluteLeft() + x;
			int dialogY = image.getAbsoluteTop() + y;

			setPopupPosition(dialogX, dialogY);

			VerticalPanel tagPanel = new VerticalPanel();

			textBox = new TextBox();
			tagPanel.add(textBox);

			textBox.addKeyPressHandler(new KeyPressHandler() {

				@Override
				public void onKeyPress(KeyPressEvent event) {
					if (event.getCharCode() == KeyCodes.KEY_ENTER) {
						saveTag(x, y, textBox);
					}

				}
			});

			Button btnTag = new Button("Tag");
			btnTag.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					saveTag(x, y, textBox);
				}
			});

			tagPanel.add(btnTag);

			setWidget(tagPanel);

		}

		public void showAndFocus() {
			show();
			textBox.setFocus(true);
		}

		private void saveTag(final int x, final int y, final TextBox textBox) {
			Tag tag = new Tag();

			tag.setPhotoKey(uploadeImage.getKey());
			tag.setBody(textBox.getValue());
			tag.setX((long) x);
			tag.setY((long) y);

			imageService.tagImage(tag, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {

				}

				@Override
				public void onSuccess(String result) {
					TagDialog.this.hide();
				}
			});
		}
	}

	@Override
	public void fireEvent(GwtEvent<?> event) {
		handlerManager.fireEvent(event);
	}

	public HandlerRegistration addGalleryUpdatedEventHandler(
			GalleryUpdatedEventHandler handler) {
		return handlerManager.addHandler(GalleryUpdatedEvent.TYPE, handler);
	}
}
